const fs = require("fs");



const log = (port, errMsg) => {
    const date = new Date();
    const year = date.getFullYear(); //获取年份
    let month = date.getMonth() + 1; //获取月份 0-11
    let day = date.getDate();          //获取当前时间的日期
    let hour = date.getHours();        //获取当前时间的小时数
    let minute = date.getMinutes();    //获取当前时间的分钟数
    let second = date.getSeconds();    //获取当前时间的秒数
    month = month < 10 ? ('0' + month) : ('' + month);
    day = day < 10 ? ('0' + day) : ('' + day);
    hour = hour < 10 ? ('0' + hour) : ('' + hour);
    minute = minute < 10 ? ('0' + minute) : ('' + minute);
    second = second < 10 ? ('0' + second) : ('' + second);

    const datetime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    const log_filename = year + "" + month + ".txt";
    let content = "[" + datetime + "]  " + port + " ---> " + errMsg.toString() + '\r\n';
    // 追加内容
    fs.appendFile("logs/" + log_filename, content, function (err) {
        if (err) {
            return console.error(err);
        }
    });
};


module.exports = log;